#pragma once

#include <iostream>
#include <pthread.h>

//RAII性质的互斥量
class Mutex
{
public:
    Mutex(pthread_mutex_t* mutex = nullptr):mutex_(mutex) { }
    void lock()
    {
        if(mutex_) pthread_mutex_lock(mutex_);
    }
    void unlock()
    {
        if(mutex_) pthread_mutex_unlock(mutex_);
    }
    ~Mutex()
    {
        //nothing
    }
private:
    pthread_mutex_t* mutex_;
};

class LockGaurd
{
public:
    LockGaurd(pthread_mutex_t* mtx):mutex_(mtx)
    {
        mutex_.lock();
    }
    ~LockGaurd()
    {
        mutex_.unlock();
    }
private:
    Mutex mutex_;
};